package com.atguigu.web;

import com.atguigu.pojo.CartItem;
import com.atguigu.pojo.Order;
import com.atguigu.pojo.User;
import com.atguigu.service.OrderItemService;
import com.atguigu.service.OrderService;
import com.atguigu.service.impl.OrderItemServiceImpl;
import com.atguigu.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class OrderServlet extends BaseServlet {

    OrderService orderService = new OrderServiceImpl();
    OrderItemService orderItemService = new OrderItemServiceImpl();

    // 创建订单
    protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        // 得到菜品车的商品项
        CartItem cartItem = (CartItem) req.getSession().getAttribute("cartItems");
        // 得到当前登录的用户id
        User user = (User) req.getSession().getAttribute("user");

        // 结算订单 将订单信息存入数据库
        Order order = orderService.createOrder(cartItem, user.getId());

        // 发送订单信息
        req.getSession().setAttribute("order", order);
        // 请求转发到订单完成页面
        resp.sendRedirect(req.getContextPath() + "/pages/cart/checkout.jsp");
    }

    // 查询当前用户的订单号
    protected void searchOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        // 1、获得用户的id
        User user = (User) req.getSession().getAttribute("user");

        // 2、通过用户id，得到用户所有之前下单的订单号
        List<Order> orderList = orderService.queryOrdersById(user.getId());

        // 3、发送订单信息和用户名
        req.getSession().setAttribute("userName", user.getUsername());
        req.getSession().setAttribute("orderList", orderList);

        // 4、请求转发到 /pages/cart/checkout.jsp
        req.getRequestDispatcher("/pages/order/order_customer.jsp").forward(req, resp);
    }

    // 查询所有的订单号
    protected void searchAllOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        // 1、查询所有的订单和用户信息
        List<Order> orderList = orderService.queryOrdersAll();

        // 2、保存订单信息到session域中
        req.getSession().setAttribute("orderList", orderList);

        // 3、请求转发到 /pages/manager/order_manager.jsp
        req.getRequestDispatcher("/pages/manager/order_manager.jsp").forward(req, resp);
    }

    // 删除订单项和订单明细
    protected void deleteOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String order_id = req.getParameter("order_id");

        // 1、删除订单明细
        orderItemService.deleteOrderItemById(order_id);

        // 2、删除订单项
        orderService.deleteOrderById(order_id);

        // 3、查询所有的订单和用户信息
        List<Order> orderList = orderService.queryOrdersAll();

        // 4、保存订单信息到session域中
        req.getSession().setAttribute("orderList", orderList);

        // 5、请求转发到 /pages/manager/order_manager.jsp
        req.getRequestDispatcher("/pages/manager/order_manager.jsp").forward(req, resp);
    }

}
